package com.ytxg.health_management.notice.controller;

import com.ytxg.health_management.controller.UserController;
import com.ytxg.health_management.notice.entity.Notice;
import com.ytxg.health_management.notice.service.NoticeService;
import com.ytxg.health_management.pojo.dto.UserDto;
import com.ytxg.health_management.pojo.entity.Result;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/notice")
public class NoticeController {
    private static final Logger log = LoggerFactory.getLogger(NoticeController.class);
    @Resource
    NoticeService noticeService;
    @Resource
    UserController userController;

    /**
     * 获取未读信息数
     */
    @GetMapping("/listNoticeNumber")
    public Result<Integer> getNoticeListNumber(@RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        Result<Integer> noticeNumber = noticeService.getNoticeListNumber(userDtoResult.getData().getId());
        return noticeNumber;
    }

    /**
     * 获取通知列表
     */
    @GetMapping("/listNotice")
    public Result<List<Notice>> getNoticeList(@RequestHeader("Authorization") String token,@RequestParam("noticeType") String noticeType) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        return noticeService.getNoticeList(noticeType,userDtoResult.getData().getId());
    }
    /**
     * 获取通知列表(管理员)
     */
    @PostMapping("/listNoticeAll")
    public Result<Map<String,Object>> getNoticeList(@RequestHeader("Authorization") String token,@RequestBody Notice notice) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        return noticeService.getNoticeListAll(notice);
    }

    /**
     * 标记已读
     */
    @GetMapping("/read")
    public Result<String> isRead(@RequestHeader("Authorization") String token,@RequestParam("noticeType") String noticeType) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);

        return noticeService.isRead(noticeType,userDtoResult.getData().getId());
    }


    @GetMapping("/unreadNotice")
    public Result<Map<String,Integer>> unreadNotice(@RequestHeader("Authorization") String token){
        Result<UserDto> userDtoResult = userController.tokenAuth(token);

        return noticeService.getUnreadNoticeNumber(userDtoResult.getData().getId());
    }
    @GetMapping("/deleteNotice")
    public Result<String> deleteNotice(@RequestParam("id") Integer id, @RequestHeader("Authorization") String token){
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        return noticeService.deleteNotice(id);
    }
    @PostMapping("/addNotice")
    public Result<String> addNotice(@RequestBody Notice notice, @RequestHeader("Authorization") String token){
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        return noticeService.addNotice(notice);
    }
}
